const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //设置验证码初始
    sendTime: '获取验证码',
    snsMsgWait: 60,
    loading: false,
    disabled: false,
    tel: '',
    captch: ''
  },
      /**
   * 手机号输入
   */
  phoneInput(e) {
    this.data.tel = e.detail.value
  },
  captchInput(e) {
    this.data.captch = e.detail.value
  },
  // 获取验证码
  sendCode: function () {
    var that = this,
      user_id = wx.getStorageSync('user_id'),
      tel = this.data.tel;
    if (user_id <= 0) {
      wx.redirectTo({
        url: '/pages/grant/grant',
      })
    }
    if (!tel || tel == '') {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    https.post('sendCode', {
      user_id: user_id,
      tel: tel,
      act: 'update',
    }, false, '验证码获取中').then(res => {
      if (res) {
        // 60秒后重新获取验证码
        var inter = setInterval(function () {
          this.setData({
            smsFlag: true,
            sendTime: this.data.snsMsgWait + 's后重发',
            snsMsgWait: this.data.snsMsgWait - 1
          });
          if (this.data.snsMsgWait < 0) {
            clearInterval(inter)
            this.setData({
              sendTime: '获取验证码',
              snsMsgWait: 60,
              smsFlag: false
            });
          }
        }.bind(this), 1000);
      }
    })
  },
  nextStep() {
    var that = this,
      user_id = wx.getStorageSync('user_id'),
      captch = this.data.captch,
      tel = this.data.tel;
    this.setData({
      loading: true,
      disabled: true
    });
    https.post('bindTel', {
      user_id: user_id,
      tel: tel,
      captch: captch
    }, false, '正在绑定').then(res => {
      if (res) {
        wx.navigateBack({
          delta: 0,
        })
      } else {
        this.setData({
          loading: false,
          disabled: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
    login_status = wx.getStorageSync('login_status');
    that.setData({
      page: 0,
      order_list: []
    })
    if (!login_status || login_status <= 0) {
      wx.showLoading({
        title: '请先进行登陆',
      })
      setTimeout(() => {
        wx.hideLoading({
          success: (res) => {
            wx.navigateTo({
              url: '/pages/grant/grant',
            })
          },
        })
      }, 1500);
    }else{
      that.getUserInfo();
    }
  },

  /**
   * @xk 获取用户信息
   */
  getUserInfo: function () {
    var that = this,
      user_id = wx.getStorageSync('user_id');
    if (user_id > 0) {
      https.post('getUserInfo', {
        user_id: user_id
      }, false, '加载中').then(res => {
        that.setData({
          user_info: res,
          tel: res.tel
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

})